package com.hllxd.hism.star.basic.service.service;

import com.alibaba.fastjson.JSON;
import com.hllxd.hism.star.basic.service.dao.http.SmartApiMapper;
import com.hllxd.hism.star.basic.service.dao.mapper.mis.SmartDqMapper;
import com.hllxd.hism.star.basic.service.model.smart.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service("smartDqService")
public class SmartDqServiceImpl implements SmartDqService {

    @Autowired
    private SmartDqMapper smartDqMapper;


    @Autowired
    private SmartApiMapper smartApiMapper;

    @Override
    public List<TblSmartDqTreeRoot> searchTableTree(String search) {
        // 获取table
        List<TblSmartDqEntity> tableList = smartDqMapper.selectAllTableInfo(search);

        // topic，table, column
        Map<String,List<TblSmartDqEntity>> root =new HashMap<>();
        for(TblSmartDqEntity table: tableList) {
            List<TblSmartDqEntity> node = root.computeIfAbsent(table.topic, k -> new ArrayList<>());
            node.add(table);
        }
        List<TblSmartDqTreeRoot> topicNodes = new ArrayList<>();
        root.forEach((topic, tables) -> {
            TblSmartDqTreeRoot topicNode = new TblSmartDqTreeRoot();
            topicNode.text = topic;
            List<TblSmartDqTreeNode> tableNodes = new ArrayList<>();
            for (TblSmartDqEntity table : tables) {
                TblSmartDqTreeNode tableNode = new TblSmartDqTreeNode();
                tableNode.text = table.tableNameCn + "("+table.tableName+")";
                tableNode.tableName = table.tableName;
                tableNode.tableNameCn = table.tableNameCn;
                tableNode.tableComment = table.tableComment;

                log.info("columns: {}",table.columnList);
                tableNode.nodes = TblSmartDqTreeLeaf.build(table.columnList);
                tableNodes.add(tableNode);
            }
            topicNode.nodes = tableNodes;

            topicNodes.add(topicNode);
        });
        return topicNodes;
    }

    @Override
    public List<TblSmartDqEntity> listLogicTable(String search) {
        log.info("search: {}.", search);
        return smartDqMapper.listLogicTable(search);
    }

    @Override
    public TblSmartDqEntity getLogicTable(Long id, String tableName) {
        if(StringUtils.isEmpty(tableName)) {
            return smartDqMapper.getLogicTable(id);
        }
        return smartDqMapper.getLogicTableByName(tableName);
    }

    @Override
    public Long setLogicTable(TblSmartDqEntity entity) {
        log.info("set logic table: {}.",entity.tableId);
        Long tableId = entity.tableId;
        if(tableId==0) {
            tableId = smartDqMapper.insertLogicTable(entity);
        }else {
            smartDqMapper.updateLogicTable(entity);
        }
        log.info("table: {}, columns: {}, id:{}.", tableId, entity.columnList, entity.id);
        List<TblSmartDqDetailEntity> detailList = TblSmartDqDetailEntity.build(tableId,entity.columnList);

        smartDqMapper.deleteLogicTableDetail(tableId);
        for(TblSmartDqDetailEntity detail: detailList) {
            log.info("column: {}", JSON.toJSONString(detail));
            smartDqMapper.insertLogicTableDetail(detail);
        }
        return tableId;
    }

    @Override
    public void delLogicTable(Long id) {
        smartDqMapper.deleteLogicTableDetail(id);
        smartDqMapper.deleteLogicTable(id);
    }

    @Override
    public List<TblSmartQueryEntity> listQueryCmd(String search) {
        return smartDqMapper.listQueryCmd(search);
    }

    @Override
    public TblSmartQueryEntity getQueryCmd(Long id, String code) {
        log.info("id: {}, code: {}.",id, code);
        if(StringUtils.isNotEmpty(code)) {
            return smartDqMapper.getQueryCmdByCode(code);
        } else {
            return smartDqMapper.getQueryCmd(id);
        }
    }

    @Override
    public Long setQueryCmd(TblSmartQueryEntity entity) {
        log.info("id: {}.", entity.id);
        if(entity.id==0) {
            return smartDqMapper.insertQueryCmd(entity);
        }else {
            return smartDqMapper.updateQueryCmd(entity);
        }
    }

    @Override
    public void delQueryCmd(Long id) {
        smartDqMapper.deleteQueryCmd(id);
    }

    @Override
    public Object query(String cmd, String config, String code, Long id, String owner) {
        Map<String,String> conf = null;
        if(StringUtils.isEmpty(config) || config.length()<4) {
            log.info("config is empty.");
        }else {
            conf = JSON.parseObject(config, Map.class);
        }
        return smartApiMapper.handle(cmd, conf, code, id, owner);
    }
}
